/*Includes*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>

#define UDP_PORT    30303
#define MEU_IP                  MAKEIP(192, 168, 0, 12)
#define MAKEIP(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | (d))

extern SOCKET mySocket;

int criarSocket(void) {

  WSADATA WSAData;
  struct sockaddr_in MySocketInfo;
  int Value;
  if(WSAStartup((short)0x0202, &WSAData) != 0) {
    puts("ERRO: Processo WSAStartup falhou.");
    return 0;
  }
  mySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(mySocket == INVALID_SOCKET) {
    puts("ERRO: Nao foi possivel criar uma conexao SOCKET.");
    WSACleanup();
    return 0;
  }
  Value = 1;
  setsockopt(mySocket, SOL_SOCKET, SO_BROADCAST, (CHAR*)&Value, sizeof(Value));
  memset(&MySocketInfo, 0, sizeof(MySocketInfo));
  MySocketInfo.sin_family = AF_INET;
  MySocketInfo.sin_port = htons(UDP_PORT);
  MySocketInfo.sin_addr.S_un.S_addr = htonl(MEU_IP);
  if((unsigned)bind(mySocket, (const struct sockaddr *)&MySocketInfo,
          sizeof(MySocketInfo)) == INVALID_SOCKET) {
    puts("ERRO: Processo bind falhou.");
    WSACleanup();
    return 0;
  }
  return 1;
}

